<?php

function __autoload($class_name) {
    include_once 'classes/' . $class_name . '.class.php';
}

$cfg = new Config;
$db = new Db;
$user = new Users($db, $cfg);
$post = new Posts($db, $cfg);
$user->checkIfLogged();

$_SESSION['uniqid'] = uniqid();
$u = $_SESSION['user'];
$usrArr = $user->getUsersTable();
//$db = new Db(DBASE);
//$db->exec("UPDATE ".USER_TBL." SET last_visit=datetime() WHERE username='".$_SESSION['user']['username']."' AND password='".$_SESSION['user']['password']."'");
$post->Paginator();

echo $_SESSION['uniqid'];
?>



<!DOCTYPE html>
<html lang="pl">
    <head>
        <meta charset="utf-8" />
        <link rel="stylesheet" type="text/css" href="css/style.css" />
        <link rel="stylesheet" type="text/css" href="css/fileuploader.css" />
        
        <title>wylęgarnia dupy</title>

    </head>
    <body>
        <div id="wrapper"><div id="page">
                <header><h1>Wylęgarnia Zwierza</h1></header>

                <div id="left">
<!--                    text form-->
<div id="text_form">
                    <form action="do.php?Posts=addPost<?= $post->getReply_To(); ?>" method="POST" enctype="multipart/form-data">

                        <textarea name="txt" onfocus="textarea_focus()" id="txtarea" rows="4" cols="20">Witaj <?= $u['username'] ?>, co dzisiaj napiszesz?</textarea>
                        <br /><input type="submit" id="submit_form" value="dopierdol" /> <input type="button" id="reset_txtarea" onClick="textarea_reset()" value="reset" />
                    </form>
    <div id="file-uploader">       
    <noscript>          
        <p>Please enable JavaScript to use file uploader.</p>
        <!-- or put a simple form for upload here -->
    </noscript>         
</div>
</div>
<!--form koniec-->
                    Posty:<br />

                    <?
                    echo '<br /><br /><br />';

                    $replies = $post->getReplies();
                    $images = $post->getImages();

                    foreach ($post->getPosts() as $v) {
                        ?>

                        <?= $v['postID'] ?> | <?= $usrArr[$v['userID']] ?> | <?= $v['timestamp'] ?> | <?= $v['uniqid'] ?><br />
                        <?= $v['txt'] ?>
                        <? foreach($images as $i){
                           if($v['uniqid'] == $i['to_post']){
                               echo '<a href=http://imgur.com/'.$i['filename'].'><img src=http://imgur.com/'.$i['filename'].'s.jpg /></a> ';
                           }
                        }?>
    <? if ($v['userID'] == $u['id']) { ?>

                            <a href="do.php?Posts=deletePost&amp;post_id=<?= $v['postID'] ?>">Usuń</a><br />

    <? } ?>
                        <a href="index.php?reply_to=<?= $v['uniqid'] ?>">Odpowiedz</a><br />

                        odpowiedzi:<br />

                        <?
                        foreach ($replies as $z) {
                            if ($v['uniqid'] == $z['reply_to']) {
                                ?>
                                <?= $z['postID'] ?> | <?= $usrArr[$z['userID']] ?> | <?= $z['timestamp'] ?> | <?= $z['reply_to'] ?> | <?= $z['uniqid'] ?><br />
            <?= $z['txt'] ?><br />
            <? foreach($images as $i){
                           if($z['uniqid'] == $i['to_post']){
                               echo '<a href=http://imgur.com/'.$i['filename'].'><img src=http://imgur.com/'.$i['filename'].'s.jpg /></a> ';
                           }
                        }?>
            <? if ($z['userID'] == $u['id']) { ?>

                                    <a href="do.php?Posts=deletePost&amp;post_id=<?= $z['postID'] ?>&amp;reply">Usuń</a><br />

                                <? } ?><br />


                            <? }
                        } ?>
                        <hr />
<? } ?>

                </div><!-- end left-->

                <div id="right">
                    <ul id="menu">
                        <li>Główna</li>
                        <li>Ustawienia</li>
                        <li>Pomoc</li>
                        <li>Admin</li>
                        <li><a href="do.php?Users=Logout">Wyloguj</a></li>
                    </ul>
                </div><!-- end right -->
            </div><!--end page-->
        </div><!--    end wrapper-->
        <script type="text/javascript" src="js/fileuploader.js" ></script>
        <script>        
        function createUploader(){            
            var uploader = new qq.FileUploader({
                element: document.getElementById('file-uploader'),
                action: 'classes/Upload.class.php',
                allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'], 
                debug: true
            });           
        }
        
        // in your app create uploader as soon as the DOM is ready
        // don't wait for the window to load  
        window.onload = createUploader;     
    </script> 
        <script type="text/javascript" src="js/script.js" ></script>
    </body>
     
</html>